
package com.kugou.game.sdk.ui.activity;

import com.kugou.framework.v4.FragmentTransaction;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.interfaces.OnGetPasswdMethodListener;
import com.kugou.game.sdk.ui.fragment.GetBackPasswdByEmailFragment;
import com.kugou.game.sdk.ui.fragment.GetBackPasswdByPhoneFragment;
import com.kugou.game.sdk.ui.fragment.GetBackPasswdByProblemFragment;
import com.kugou.game.sdk.utils.Rx;

import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 描述:找回密码主界面
 *
 * @author wcj
 * @since 2015年8月13日 上午11:05:45
 */
public class GetBackPasswordActivity extends BaseCommonTitleFragmentActivity implements
        OnClickListener {

    /**
     * 显示输入用户名界面
     */
    private static final int MSG_SHOW_ENTER_USER_VIEW = 10000;

    /**
     * 显示选择方法界面
     */
    private static final int MSG_SHOW_SELECT_METHOD_VIEW = 10001;

    /**
     * 显示具体方法界面
     */
    private static final int MSG_SHOW_METHOD_VIEW = 10002;

    private static final int MSG_REQUEST_GET_PASSWD_METHOD = 10004;

    private static final int MSG_REQUEST_GET_PASSWD_METHOD_SUCCESS = 10005;

    private static final int MSG_REQUEST_GET_PASSWD_METHOD_FAILED = 10006;

    RelativeLayout mEnterUserNameLayout, mPhoneLayout, mEmailLayout, mProblemLayout,
            mServiceLayout;

    LinearLayout mSelectLayout;

    FrameLayout mFrameLayout;

    GetBackPasswdByPhoneFragment mGetBackPasswdByPhoneFragment;

    GetBackPasswdByEmailFragment mGetBackPasswdByEmailFragment;

    GetBackPasswdByProblemFragment mGetBackPasswdByProblemFragment;

    EditText mUserNameEditText;

    String mUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int layoutId;
        if (SDKControler.getActivityOrientation() == ActivityOrientation.PORTRAIT) {
            layoutId = Rx.layout.kg_activity_get_back_password;
        } else {
            layoutId = Rx.layout.kg_activity_get_back_password_land;
        }
        if (layoutId != 0) {
            setContentView(layoutId);
            setCommonTitle("取回密码");
            initViews();
        } else {
            showToast(Rx.string.kg_layout_not_found);
        }
    }

    private void initViews() {
        mEnterUserNameLayout = (RelativeLayout) findViewById(Rx.id.kg_rl_enter_username);

        mUserNameEditText = (EditText) findViewById(Rx.id.kg_et_username);

        Button nextStepBtn = (Button) findViewById(Rx.id.kg_btn_next_step);
        nextStepBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendEmptyBackgroundMessage(MSG_REQUEST_GET_PASSWD_METHOD);
            }
        });

        mFrameLayout = (FrameLayout) findViewById(Rx.id.kg_container);

        mSelectLayout = (LinearLayout) findViewById(Rx.id.kg_ll_select_get_back_method);
        mPhoneLayout = (RelativeLayout) findViewById(Rx.id.kg_rl_get_back_by_phone);
        mPhoneLayout.setOnClickListener(this);
        mEmailLayout = (RelativeLayout) findViewById(Rx.id.kg_rl_get_back_by_email);
        mEmailLayout.setOnClickListener(this);
        mProblemLayout = (RelativeLayout) findViewById(Rx.id.kg_rl_get_back_by_problem);
        mProblemLayout.setOnClickListener(this);
        mServiceLayout = (RelativeLayout) findViewById(Rx.id.kg_rl_get_back_by_service);

        TextView phoneTv = (TextView) findViewById(Rx.id.kg_tv_service_phone);
        String servicePhone = getString(Rx.string.kg_custom_service_phone);
        phoneTv.setText(Html.fromHtml("<font color=#ff333333>客服电话：</font>" + "<font color=#249EF4>"
                + servicePhone + "</font>"));
        phoneTv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showPhoneDialog();
            }
        });

        TextView qqTv = (TextView) findViewById(Rx.id.kg_tv_service_qq);
        String qq = getString(Rx.string.kg_custom_service_qqnum);
        qqTv.setText(Html.fromHtml("<font color=#ff333333>客服QQ：</font>" + "<font color=#249EF4>"
                + qq + "</font>"));
        qqTv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showQQDialog();
            }
        });

        initFragment();
    }

    private void initFragment() {
        mGetBackPasswdByPhoneFragment = GetBackPasswdByPhoneFragment.createFragment();
        mGetBackPasswdByEmailFragment = GetBackPasswdByEmailFragment.createFragment();
        mGetBackPasswdByProblemFragment = new GetBackPasswdByProblemFragment();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(Rx.id.kg_container, mGetBackPasswdByPhoneFragment);
        transaction.add(Rx.id.kg_container, mGetBackPasswdByEmailFragment);
        transaction.add(Rx.id.kg_container, mGetBackPasswdByProblemFragment);
        transaction.commitAllowingStateLoss();
    }

    private void requestGetMethod() {
        mUserName = mUserNameEditText.getText().toString().trim();
        if (TextUtils.isEmpty(mUserName)) {
            showToast("请输入用户名");
            return;
        }

        com.kugou.game.sdk.core.UserManager.getInstance().getPasswdMethod(mUserName,
                new OnGetPasswdMethodListener() {

                    @Override
                    public void onSuccess(String email, String mobile, int questionId) {
                        Message msg = Message.obtain();
                        msg.what = MSG_REQUEST_GET_PASSWD_METHOD_SUCCESS;
                        Bundle bundle = new Bundle();
                        bundle.putString("email", email);
                        bundle.putString("mobile", mobile);
                        bundle.putInt("questionId", questionId);
                        msg.setData(bundle);
                        sendUiMessage(msg);

                    }

                    @Override
                    public void onFailed(String errorMsg) {
                        Message msg = Message.obtain();
                        msg.what = MSG_REQUEST_GET_PASSWD_METHOD_FAILED;
                        msg.obj = errorMsg;
                        sendUiMessage(msg);

                    }
                });

    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);

        switch (msg.what) {
            case MSG_SHOW_ENTER_USER_VIEW:
                mEnterUserNameLayout.setVisibility(View.VISIBLE);
                mSelectLayout.setVisibility(View.GONE);
                mFrameLayout.setVisibility(View.GONE);

                mPhoneLayout.setVisibility(View.GONE);
                mEmailLayout.setVisibility(View.GONE);
                mProblemLayout.setVisibility(View.GONE);
                break;

            case MSG_SHOW_SELECT_METHOD_VIEW:
                mEnterUserNameLayout.setVisibility(View.GONE);
                mSelectLayout.setVisibility(View.VISIBLE);
                mFrameLayout.setVisibility(View.GONE);
                break;

            case MSG_SHOW_METHOD_VIEW:
                mEnterUserNameLayout.setVisibility(View.GONE);
                mSelectLayout.setVisibility(View.GONE);
                mFrameLayout.setVisibility(View.VISIBLE);

                break;
            case MSG_REQUEST_GET_PASSWD_METHOD_FAILED:
                showToast((String) msg.obj);
                break;

            case MSG_REQUEST_GET_PASSWD_METHOD_SUCCESS:
                sendEmptyUiMessage(MSG_SHOW_SELECT_METHOD_VIEW);

                if (msg.getData() != null) {
                    if (!TextUtils.isEmpty(msg.getData().getString("email"))) {
                        mEmailLayout.setVisibility(View.VISIBLE);
                        mGetBackPasswdByEmailFragment.setData(mUserName,
                                msg.getData().getString("email"));
                    }

                    if (!TextUtils.isEmpty(msg.getData().getString("mobile"))) {
                        mPhoneLayout.setVisibility(View.VISIBLE);
                        mGetBackPasswdByPhoneFragment.setData(mUserName,
                                msg.getData().getString("mobile"));
                    }

                    int questionId = msg.getData().getInt("questionId");

                    if (questionId != 255 && questionId >= 0) {
                        mProblemLayout.setVisibility(View.VISIBLE);
                        mGetBackPasswdByProblemFragment.setData(mUserName, questionId);
                    }
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        super.handleBackgroundMessage(msg);

        switch (msg.what) {
            case MSG_REQUEST_GET_PASSWD_METHOD:
                hideSoftInput(this);
                requestGetMethod();
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mFrameLayout.getVisibility() == View.VISIBLE) {
            sendEmptyUiMessage(MSG_SHOW_SELECT_METHOD_VIEW);
            return;
        } else if (mSelectLayout.getVisibility() == View.VISIBLE) {
            sendEmptyUiMessage(MSG_SHOW_ENTER_USER_VIEW);
            return;
        }

        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        sendEmptyUiMessage(MSG_SHOW_METHOD_VIEW);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(mGetBackPasswdByEmailFragment).hide(mGetBackPasswdByPhoneFragment)
                .hide(mGetBackPasswdByProblemFragment);
        if (v.getId() == Rx.id.kg_rl_get_back_by_phone) {
            transaction.show(mGetBackPasswdByPhoneFragment);
        } else if (v.getId() == Rx.id.kg_rl_get_back_by_problem) {
            mGetBackPasswdByProblemFragment.init();
            transaction.show(mGetBackPasswdByProblemFragment);
        } else if (v.getId() == Rx.id.kg_rl_get_back_by_email) {
            transaction.show(mGetBackPasswdByEmailFragment);
        }

        transaction.commitAllowingStateLoss();

    }
}
